package com.kandinfo.survey.questionnaire.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kandinfo.base.common.converter.ConverterService;
import com.kandinfo.base.common.dto.ResultDto;
import com.kandinfo.base.common.dto.ResultDtoFactory;
import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.base.security.SecurityContext;
import com.kandinfo.base.security.domain.User;
import com.kandinfo.survey.common.constants.StatusConstant;
import com.kandinfo.survey.common.domain.QuestionnaireInfo;
import com.kandinfo.survey.common.repository.QuestionnaireInfoRepository;
import com.kandinfo.survey.questionnaire.dto.QuestionnaireInfoRequestDto;
import com.kandinfo.survey.questionnaire.service.QuestionnaireInfoService;

@Service
public class QuestionnaireInfoServiceImpl extends AbstractService<QuestionnaireInfo, Long, QuestionnaireInfoRepository>
        implements QuestionnaireInfoService {

    private static final Logger logger = LoggerFactory.getLogger(QuestionnaireInfoServiceImpl.class);

    @Resource
    public void setQuestionnaireInfoRepository(QuestionnaireInfoRepository questionnaireInfoRepository) {
        super.setRepository(questionnaireInfoRepository);
    }

    @Override
    @Transactional
    public ResultDto newQuestionnaire(QuestionnaireInfoRequestDto questionnaireInfoDto) {
        QuestionnaireInfo domain = new QuestionnaireInfo();
        try {
            domain = ConverterService.convert(questionnaireInfoDto, QuestionnaireInfo.class);
        } catch (Exception e) {
            logger.error("newQuestionnaire convert error - QuestionnaireInfoServiceImpl - newQuestionnaire ",
                    e.toString());
            return ResultDtoFactory.toNack(" 新建失败! ");
        }
        repository.save(domain);

        return ResultDtoFactory.toAckNoCode(" 新建成功! ");
    }

    @Override
    @Transactional
    public ResultDto editQuestionnaire(QuestionnaireInfoRequestDto questionnaireInfoDto) {
        Long questionnaireId = questionnaireInfoDto.getQuestionnaireId();
        if (questionnaireId == null) {
            return ResultDtoFactory.toNack(" questionnaireId不能为空! ");
        }
        QuestionnaireInfo domain = repository.findOne(questionnaireInfoDto.getQuestionnaireId());
        if (domain == null) {
            return ResultDtoFactory.toNack(" 找不到试卷信息! ");
        }

        // do edit
        User currentUser = SecurityContext.getInstance().getCurrentUser();
        domain.setUpdateTs(new Date());
        domain.setUpdateBy(currentUser.getId());
        domain.setDesc(questionnaireInfoDto.getDesc());
        domain.setDestType(questionnaireInfoDto.getDestType());
        domain.setName(questionnaireInfoDto.getName());
        domain.setStatus(questionnaireInfoDto.getStatus());
        repository.saveAndFlush(domain);

        return ResultDtoFactory.toAckNoCode(" 问卷修改成功! ");
    }

    @Override
    @Transactional
    public ResultDto deleteQuestionnaire(QuestionnaireInfoRequestDto questionnaireInfoDto) {
        QuestionnaireInfo domain = repository.findOne(questionnaireInfoDto.getQuestionnaireId());
        if (domain == null) {
            return ResultDtoFactory.toNack(" 找不到相应的问卷信息! ", Boolean.FALSE);
        }

        // 置为逻辑删除
        domain.setStatus(StatusConstant.TOMBSTONED);
        repository.saveAndFlush(domain);
        return ResultDtoFactory.toAck(" 问卷下线成功! ", Boolean.TRUE);
    }

}
